package com.springboot.ball.service.impl.ranklist;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.springboot.ball.mapper.UserMapper;
import com.springboot.ball.pojo.User;
import com.springboot.ball.service.ranklist.GetRanklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetRanklistServiceImpl implements GetRanklistService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public JSONObject getRanklist(Integer page, Integer pageSize) {  // 添加pageSize参数
        IPage<User> userIPage = new Page<>(page, pageSize);  // 使用传入的pageSize
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("rating");

        // 仅执行一次查询
        userIPage = userMapper.selectPage(userIPage, queryWrapper);

        // 隐藏密码字段
        userIPage.getRecords().forEach(user -> user.setPassword(""));

        JSONObject resp = new JSONObject();
        resp.put("users", userIPage.getRecords());
        resp.put("total", userIPage.getTotal());  // 确保返回total字段
        resp.put("pages", userIPage.getPages());  // 返回总页数
        return resp;
    }
}